<?php
namespace app\manage\controller;

use think\Db;
class Article extends Base
{
    //分类列表
    public function cateList() {
        $curr_page = input('post.pageIndex',1);
        $perpage = input('post.pageSize',10);
        $where = [
            ['del','=',0]
        ];
        try {
            $count = Db::table('mp_article_cate')->where($where)->count();
            $list = Db::table('mp_article_cate')->where($where)->order(['id'=>'DESC'])->limit(($curr_page - 1)*$perpage,$perpage)->select();
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        $ret = [
            'count' => $count,
            'list' => $list
        ];
        return ajax($ret);
    }
    //文章列表
    public function articleList() {
        $search = input('post.search');
        $cate_id = input('post.cate_id');
        $curr_page = input('post.pageIndex',1);
        $perpage = input('post.pageSize',10);
        $where = [
            ['a.del','=',0]
        ];
        if($cate_id) {
            $where[] = ['a.cate_id','=',$cate_id];
        }
        if($search) {
            $where[] = ['a.title','like',"%{$search}%"];
        }
        try {
            $count = Db::table('mp_article')->alias('a')->where($where)->count();
            $page['count'] = $count;
            $page['curr'] = $curr_page;
            $page['totalPage'] = ceil($count/$perpage);
            $list = Db::table('mp_article')->alias('a')
                ->join('mp_article_cate c','a.cate_id=c.id','left')
                ->where($where)
                ->limit(($curr_page - 1)*$perpage,$perpage)
                ->order(['a.id'=>'DESC'])
                ->field('a.id,a.pic,a.title,a.status,c.cate_name')
                ->select();
        }catch (\Exception $e) {
            return ajax('SQL错误: ' . $e->getMessage(),-1);
        }
        $ret = [
            'list' => $list,
            'count' => $count
        ];
        return ajax($ret);
    }
    //文章详情
    public function articleDetail() {
        $post['id'] = input('post.id');
        checkPost($post);
        try {
            $where = [
                ['id','=',$post['id']],
                ['del','=',0]
            ];
            $info = Db::table('mp_article')->where($where)->find();
            if(!$info) {
                return ajax($post['id'],-4);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        $info['content'] = $this->editorAddPrefix($info['content']);
        return ajax($info);
    }
//添加文章POST
    public function articleAdd() {
        return ajax('',-1,'请使用新版本后台操作');

        $post['cate_id'] = input('post.cate_id');
        $post['title'] = input('post.title');
        $post['status'] = input('post.status');
        checkPost($post);
        $post['content'] = input('post.content');
        $post['content'] = $this->editorRemovePrefix($post['content']);
        $time = time();
        $post['create_time'] = $time;
        $tmp_image = input('post.path','');
        try {
            if(!$tmp_image) {
                return ajax('请上传图片',3);
            }
            $oss_exist = $this->existObject($tmp_image);
            if($oss_exist !== true) {
                return ajax('图片已失效请重新上传',70);
            }
            $image_move = $this->moveObject($tmp_image,'upload/article/');
            if($image_move['code'] == 0) {
                $post['pic'] = $image_move['path'];
            }else {
                return ajax('OSS异常',72);
            }
            Db::table('mp_article')->insertGetId($post);
        }catch (\Exception $e) {
            if(isset($post['pic'])) {
                $this->deleteObject($post['pic']);
            }
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }
//修改文章POST
    public function articleMod() {
        return ajax('',-1,'请使用新版本后台操作');
        $post['cate_id'] = input('post.cate_id');
        $post['title'] = input('post.title');
        $post['status'] = input('post.status');
        $post['id'] = input('post.id');
        checkPost($post);
        $post['content'] = input('post.content');
        $post['content'] = $this->editorRemovePrefix($post['content']);
//        $time = time();
//        $post['create_time'] = $time;
        $tmp_image = input('post.path','');
        try {
            $where = [
                ['id','=',$post['id']]
            ];
            $exist = Db::table('mp_article')->where($where)->field('pic')->find();
            if(!$exist) {
                return ajax('id',-4);
            }
            $old_pic = $exist['pic'];
            if(!$tmp_image) {
                return ajax('请上传图片',3);
            }
            $oss_exist = $this->existObject($tmp_image);
            if($oss_exist !== true) {
                return ajax('图片已失效请重新上传',70);
            }
            $image_move = $this->moveObject($tmp_image,'upload/article/');
            if($image_move['code'] == 0) {
                $post['pic'] = $image_move['path'];
            }else {
                return ajax('OSS异常',72);
            }
            Db::table('mp_article')->where($where)->update($post);
        }catch (\Exception $e) {
            if(isset($post['pic']) && $post['pic'] != $old_pic) {
                $this->deleteObject($post['pic']);
            }
            return ajax($e->getMessage(),-1);
        }
        if(isset($post['pic']) && $post['pic'] != $old_pic) {
            $this->deleteObject($old_pic);
        }
        return ajax();
    }
    //删除文章...
    public function articleDelete() {
        return ajax('',-1,'请使用新版本后台操作');

        $post['id'] = input('post.id','');
        checkPost($post);
        $where = [
            ['id','=',$post['id']]
        ];
        try {
            Db::table('mp_article')->where($where)->update(['del'=>1]);
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }

    //显示隐藏
    public function articleStatus() {
        $id = input('post.id','0');
        $map = [
            ['id','=',$id]
        ];
        try {
            $exist = Db::table('mp_article')->where($map)->find();
            if($exist['status'] == 1) {
                Db::table('mp_article')->where($map)->update(['status'=>0]);
            }else {
                Db::table('mp_article')->where($map)->update(['status'=>1]);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }

//添加分类
    public function cateAdd() {
        $val['cate_name'] = input('post.cate_name');
        $val['status'] = input('post.status');
        checkPost($val);
        try {
            Db::table('mp_article_cate')->insert($val);
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }
//分类详情
    public function cateDetail() {
        $post['id'] = input('post.id','');
        try {
            $whereCate = [
                ['id','=',$post['id']]
            ];
            $info = Db::table('mp_article_cate')->where($whereCate)->find();
            if(!$info) {
                return ajax('非法参数',-4);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax($info);
    }
//修改分类POST
    public function cateMod() {
        $val['cate_name'] = input('post.cate_name');
        $val['status'] = input('post.status');
        $val['id'] = input('post.id');
        checkPost($val);
        try {
            $whereCate = [
                ['id','=',$val['id']]
            ];
            $cate_exist = Db::table('mp_article_cate')->where($whereCate)->find();
            if(!$cate_exist) {
                return ajax('非法参数',-4);
            }
            Db::table('mp_article_cate')->where($whereCate)->update($val);
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }
//删除分类
    public function cateDelete() {
        $post['id'] = input('post.id');
        try {
            $whereCate = [
                ['id','=',$post['id']]
            ];
            $exist = Db::table('mp_article_cate')->where($whereCate)->find();
            if(!$exist) {
                return ajax('非法参数',-1);
            }
            Db::table('mp_article_cate')->where($whereCate)->update(['del'=>1]);
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }


}